home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / CWhoisEngine 1.0 / CWhoisEngine.c next >
C/C++ Source or Header  |  1992-07-01  |  5KB  |  195 lines

  1. #include "CWhoisEngine.h"
  2. #include "string.h"
  3. #include <Exceptions.h>
  4. #include "TCLUtilities.h"
  5. #include "Global.h"
  6.  
  7. void CWhoisEngine::IWhoisEngine(long rcvBuffSize, long wdsBuffSize)
  8. {
  9.     TRY
  10.     {
  11.         /* Give function deep access to memory reserves.  */
  12.         
  13.         SetAllocation(FALSE);
  14.         SetCriticalOperation(TRUE);
  15.         
  16.         /*  Lock object reference to keep member variables valid.  */
  17.         
  18.         HLock(this);
  19.     
  20.         /*  Initialize the wds buffer  */
  21.         
  22.         wdsBuffer = NewPtr(wdsBuffSize);
  23.         FailMemError();
  24.         
  25.         /*  Initialize MacTCP and the tcp stream  */
  26.         
  27.         tcp.ioNamePtr = (Ptr) "\P.IPP";
  28.         tcp.ioCompletion = NULL;
  29.         FailOSErr(PBOpen(&tcp, FALSE));                    // Open MacTCP drivers
  30.         
  31.         tcp.csParam.create.rcvBuff = NewPtr(rcvBuffSize);
  32.         FailMemError();
  33.         tcp.csParam.create.rcvBuffLen = rcvBuffSize;
  34.         tcp.csCode = TCPCreate;
  35.         FailOSErr(PBControl(&tcp, FALSE));                    // Create a TCP stream
  36.         
  37.         /* Unlock object.  */
  38.         
  39.         HUnlock(this);
  40.         
  41.         /*  Restore normal memory management.  */
  42.         
  43.         SetCriticalOperation(FALSE);
  44.         SetAllocation(TRUE);
  45.     }
  46.     CATCH
  47.     {
  48.         if (wdsBuffer) DisposPtr(wdsBuffer);
  49.         if (tcp.csParam.create.rcvBuff) DisposPtr(tcp.csParam.create.rcvBuff);
  50.     }
  51.     ENDTRY;
  52. }
  53.  
  54. void CWhoisEngine::SetWhoisServer(ip_addr theAddr)
  55. {
  56.     whoisServer = theAddr;
  57. }
  58.  
  59. char* CWhoisEngine::Whois(char* searchTag)
  60. {
  61.     char* marker;                //  Initialized during string construction
  62.     char* answer = NULL;
  63.     long answerSize = 1;        //  SIze of terminating null
  64.     RWDS rwds;
  65.     
  66.     TRY
  67.     {
  68.         /* Give function deep access to memory reserves.  */
  69.         
  70.         SetAllocation(FALSE);
  71.         SetCriticalOperation(TRUE);
  72.         
  73.         /*  Lock object reference to keep member variables valid.  */
  74.         
  75.         HLock(this);
  76.         
  77.         /*  Open a connection to the whois server on the existing TCP stream.  */
  78.                 
  79.         tcp.csParam.open.remoteHost = whoisServer;
  80.         tcp.csParam.open.remotePort = 43;            // port reserved for whois
  81.         tcp.csParam.open.localPort = 0;                // use any unused local port
  82.         tcp.csCode = TCPActiveOpen;                    // open the conection
  83.         FailOSErr(PBControl(&tcp, FALSE));
  84.                 
  85.         /*  Copy the characters to the write buffer, append a carriage return
  86.             and newline (to conform to whois specs).  */
  87.             
  88.         rwds.length = strlen(searchTag);
  89.         strcpy(wdsBuffer, searchTag);
  90.         wdsBuffer[rwds.length++] = '\r';
  91.         wdsBuffer[rwds.length++] = '\n';
  92.         wdsBuffer[rwds.length++] = '\0';
  93.         rwds.buffer = wdsBuffer;
  94.                 
  95.         /*  Terminate the rwds  */
  96.         
  97.         rwds.termination = 0;
  98.         
  99.         /*  Send the formatted string to the whois server.  */
  100.                 
  101.         tcp.csParam.send.wdsPtr = (Ptr) &rwds;
  102.         tcp.csCode = TCPSend;
  103.         FailOSErr(PBControl(&tcp, FALSE));
  104.                 
  105.         /*  Receive the data from the whois server.  As the amount of data
  106.             may be more than the allocated buffer, check if all of the data
  107.             has been received and cycle back as necessary.  */
  108.     
  109.         do
  110.         {
  111.             /*  Receive the data.  */
  112.         
  113.             tcp.csParam.receive.rdsPtr = (Ptr) &rwds;
  114.             tcp.csParam.receive.rdsLength = 1;            //  1 buffer in rds
  115.             tcp.csCode = TCPNoCopyRcv;                    //  Use MacTCP's internal buffers
  116.             FailOSErr(PBControl(&tcp, FALSE));
  117.         
  118.             /*  Build the results string.  */
  119.     
  120.             marker = answer;
  121.             answer = NewPtr(rwds.length + answerSize--);
  122.             FailMemError();
  123.             if (marker)
  124.             {
  125.                 BlockMove(marker, answer, answerSize);    //  Copy old data to new block of memory
  126.                 FailMemError();
  127.                 DisposPtr(marker);                        //  and release the memory it used
  128.                 FailMemError();
  129.             }
  130.             BlockMove(rwds.buffer, answer + answerSize, rwds.length);
  131.             FailMemError();
  132.             answerSize += rwds.length;
  133.             answer[answerSize] = '\0';    //  Whois data is not null-terminated.
  134.             
  135.             /*  Return the buffers to MacTCP.  */
  136.             
  137.             tcp.csCode = TCPRcvBfrReturn;
  138.             FailOSErr(PBControl(&tcp, FALSE));
  139.         
  140.             /*  Check to see if there is any unread data.  */
  141.                     
  142.             tcp.csCode = TCPStatus;
  143.             FailOSErr(PBControl(&tcp, FALSE));
  144.         }
  145.         while(tcp.csParam.status.connectionState != 14);
  146.     
  147.         /*  Close the connection to the whois server.  */
  148.                 
  149.         tcp.csCode = TCPClose;
  150.         FailOSErr(PBControl(&tcp, FALSE));
  151.         
  152.         /*  Unlock object.  */
  153.         
  154.         HUnlock(this);
  155.     }
  156.     CATCH
  157.     {
  158.         tcp.csCode = TCPAbort;    // Abort all i/o on this stream and close connection
  159.         PBControl(&tcp, FALSE);
  160.         if (marker) ForgetPtr(marker);
  161.         if (answer) ForgetPtr(answer);
  162.     }
  163.     ENDTRY;
  164.         
  165.     /*  Restore normal memory management.  */
  166.         
  167.     SetCriticalOperation(FALSE);
  168.     SetAllocation(TRUE);
  169.     
  170.     /*  Return the results string.  */
  171.     
  172.     return answer;
  173. }
  174.  
  175. void CWhoisEngine::Dispose(void)
  176. {
  177.     /*  Lock object reference to keep member variables valid.  */
  178.     
  179.     HLock(this);
  180.     
  181.     /*  Release the buffers used by MacTCP.  */
  182.     
  183.     tcp.csCode = TCPRelease;
  184.     PBControl(&tcp, FALSE);
  185.     DisposPtr(tcp.csParam.create.rcvBuff);
  186.     
  187.     /*  Release the wds buffer.  */
  188.     
  189.     DisposPtr(wdsBuffer);
  190.     
  191.     /*  Unlock object and delete it.  */
  192.     
  193.     HUnlock(this);
  194.     inherited::Dispose();
  195. }